/*
* jQuery jBox 2.3
*/

(function($) {
	var getArgs = function(args,name){
		if(args){
			var p = args.split('&'), a;
			for( var i=0,l = p.length; i < l; i++ ){
				a = p[i].split('=');
				if( name === a[0] ) return a[1];
			}
		}
		return null;
	}
	var createLink = function(charset){
		var script=window["document"]["getElementsByTagName"]("script");
		var D=window["document"]["getElementsByTagName"]("HEAD").item(0);
		var _args="",A="";
		for(var i=0; i < script.length; i++ ){
			me = !!document.querySelector ? script[i].src : script[i].getAttribute('src',4);
			if( me.substr(me.lastIndexOf('/')).indexOf('jquery.jBox') !== -1 )
				break;
		}
		me = me.split('?');
		_args = me[1];
		A=me[0].substr( 0, me[0].lastIndexOf('/') + 1 );
		var skin=getArgs(_args,"skin") || "Default";
		var link=window["document"].createElement("link");
		if(D){
			link.id="skin";
			link.href=A + "Skins/"+skin+"/jbox.css";
			link.rel="stylesheet";
			link.type="text/css";
			if(charset)_.charset=charset;
			D.appendChild(link)
		}
	}
	createLink();	
	$.jBox = function(content, options,callback) {
		options = $.extend({}, $.jBox.defaults, options);
		options.showFade = options.opacity > 0;
		options.isTip = options.isTip || false;
		options.isMessager = options.isMessager || false;
		if (content == undefined) {
			content = ''
		};
		if (options.border < 0) {
			options.border = 0
		};
		if (options.id == undefined) {
			options.id = 'jBox_' + Math.floor(Math.random() * 1000000)
		};
		//var isIE6 = ($.browser.msie && parseInt($.browser.version) < 7);
		var browser = /(msie) ([\w.]+)/.exec((navigator.userAgent).toLowerCase());
		if(browser){
			var isIE = 	browser[1]=="msie";
			var isIE6 = (isIE && parseInt(browser[2]) < 7);
		}
		var $jBoxId = $('#' + options.id);
		if ($jBoxId.length > 0) {
			options.zIndex = $.jBox.defaults.zIndex++;
			$jBoxId.css({
				zIndex: options.zIndex
			});
			$jBoxId.find('#jbox').css({
				zIndex: options.zIndex + 1
			});
			return $jBoxId
		};
		var configs = {
			url: '',
			type: '',
			html: '',
			isObject: content.constructor == Object
		};
		if (!configs.isObject) {
			content = content + '';
			var toLower = content.toLowerCase();
			if (toLower.indexOf('id:') == 0) configs.type = 'ID';
			else if (toLower.indexOf('get:') == 0) configs.type = 'GET';
			else if (toLower.indexOf('post:') == 0) configs.type = 'POST';
			else if (toLower.indexOf('iframe:') == 0) configs.type = 'IFRAME';
			else if (toLower.indexOf('html:') == 0) configs.type = 'HTML';
			else {
				content = 'html:' + content;
				configs.type = 'HTML'
			};
			content = content.substring(content.indexOf(":") + 1, content.length)
		};
		if (!options.isTip && !options.isMessager && !options.showScrolling) {
			$(isIE ? 'html': 'body').attr('style', 'overflow:hidden;padding-right:17px;')
		};
		var hasTitlePanel = !options.isTip && !(options.title == undefined);
		var isExt = configs.type == 'GET' || configs.type == 'POST' || configs.type == 'IFRAME';
		var titleWidth = typeof options.width == 'number' ? (options.width - 50) + 'px': "90%";
		var struHtml = [];
		struHtml.push('<div id="' + options.id + '" class="jbox-' + (options.isTip ? 'tip': (options.isMessager ? 'messager': 'body')) + '">');
		if (options.showFade) {
			if ((isIE6 && $('iframe').length > 0) || $('object, applet').length > 0) {
				struHtml.push('<iframe id="jbox-fade" class="jbox-fade" src="about:blank" style="display:block;position:absolute;z-index:-1;"></iframe>')
			} else {
				if (isIE6) {
					$('select').css('visibility', 'hidden')
				};
				struHtml.push('<div id="jbox-fade" class="jbox-fade" style="position:absolute;"></div>')
			}
		};
		struHtml.push('<div id="jbox-temp" class="jbox-temp" style="width:0px;height:0px;background-color:#ff3300;position:absolute;z-index:1984;fdisplay:none;"></div>');
		if (options.draggable) {
			struHtml.push('<div id="jbox-drag" class="jbox-drag" style="position:absolute;z-index:1984;display:none;"></div>')
		};
		struHtml.push('<div id="jbox" class="jbox" style="position:absolute;width:auto;height:auto;">');
		struHtml.push('<div class="jbox-help-title jbox-title-panel" style="height:25px;display:none;"></div>');
		struHtml.push('<div class="jbox-help-button jbox-button-panel" style="height:25px;padding:10px 0 10px 0;display:none;"></div>');
		struHtml.push('<table border="0" cellpadding="0" cellspacing="0" style="margin:0px;padding:0px;border:none;">');
		if (options.border > 0) {
			struHtml.push('<tr>');
			struHtml.push('<td class="jbox-border" style="margin:0px;padding:0px;border:none;border-radius:' + options.border + 'px 0 0 0;width:' + options.border + 'px;height:' + options.border + 'px;"></td>');
			struHtml.push('<td class="jbox-border" style="margin:0px;padding:0px;border:none;height:' + options.border + 'px;overflow: hidden;"></td>');
			struHtml.push('<td class="jbox-border" style="margin:0px;padding:0px;border:none;border-radius:0 ' + options.border + 'px 0 0;width:' + options.border + 'px;height:' + options.border + 'px;"></td>');
			struHtml.push('</tr>')
		};
		struHtml.push('<tr>');
		struHtml.push('<td class="jbox-border" style="margin:0px;padding:0px;border:none;"></td>');
		struHtml.push('<td valign="top" style="margin:0px;padding:0px;border:none;">');
		struHtml.push('<div class="jbox-container" style="width:auto; height:auto;">');
		struHtml.push('<a class="jbox-close" title="' + $.jBox.languageDefaults.close + '" onmouseover="$(this).addClass(\'jbox-close-hover\');" onmouseout="$(this).removeClass(\'jbox-close-hover\');" style="position:absolute; cursor:pointer; top:' + (options.border) + 'px; right:' + (10 + options.border) + 'px;' + (options.showClose ? '': 'display:none;') + '"></a>');
		if (hasTitlePanel) {
			struHtml.push('<div class="jbox-title-panel" style="height:35px;">');
			struHtml.push('<div class="jbox-title' + (options.showIcon == true ? ' jbox-title-icon': (options.showIcon == false ? '': ' ' + options.showIcon)) + '" style="float:left; width:' + titleWidth + '; line-height:' + (isIE ? 35: 34) + 'px;overflow:hidden;text-overflow:ellipsis;word-break:break-all;">' + (options.title == '' ? '&nbsp;': options.title) + '</div>');
			struHtml.push('</div>')
		};
		struHtml.push('<div id="jbox-states"></div></div>');
		struHtml.push('</div>');
		struHtml.push('</td>');
		struHtml.push('<td class="jbox-border" style="margin:0px;padding:0px;border:none;"></td>');
		struHtml.push('</tr>');
		if (options.border > 0) {
			struHtml.push('<tr>');
			struHtml.push('<td class="jbox-border" style="margin:0px;padding:0px;border:none;border-radius:0 0 0 ' + options.border + 'px; width:' + options.border + 'px; height:' + options.border + 'px;"></td>');
			struHtml.push('<td class="jbox-border" style="margin:0px;padding:0px;border:none;height:' + options.border + 'px;overflow: hidden;"></td>');
			struHtml.push('<td class="jbox-border" style="margin:0px;padding:0px;border:none;border-radius:0 0 ' + options.border + 'px 0; width:' + options.border + 'px; height:' + options.border + 'px;"></td>');
			struHtml.push('</tr>')
		};
		struHtml.push('</table>');
		struHtml.push('</div>');
		struHtml.push('</div>');
		var iframeHtml = '<iframe name="jbox-iframe" id="jbox-iframe" width="100%" height="100%" marginheight="0" marginwidth="0" frameborder="0" scrolling="' + options.iframeScrolling + '"></iframe>';
		var $win = $(window);
		var $docBody = $(document.body);
		var $struObj = $(struHtml.join('')).appendTo($docBody);
		var $box = $struObj.children('#jbox');
		var $fade = $struObj.children('#jbox-fade');
		var $temp = $struObj.children('#jbox-temp');
		if (!configs.isObject) {
			switch (configs.type) {
			case "ID":
				configs.html = $('#' + content).html();
				break;
			case "GET":
			case "POST":
				configs.html = '';
				configs.url = content;
				break;
			case "HTML":
				configs.html = content;
				break;
			case "IFRAME":
				configs.html = iframeHtml;
				if (content.indexOf('#') == -1) {
					configs.url = content + (content.indexOf('?') == -1 ? '?___t': '&___t') + Math.random()
				} else {
					var urlArr = content.split('#');
					configs.url = urlArr[0] + (urlArr[0].indexOf('?') == -1 ? '?___t': '&___t') + Math.random() + '#' + urlArr[1]
				};
				break
			};
			content = {
				state0: {
					content: configs.html,
					buttons: options.buttons,
					buttonsFocus: options.buttonsFocus,
					submit: options.submit
				}
			}
		};
		var stateItem = [];
		var h1 = $box.find('.jbox-help-title').outerHeight(true);
		var h2 = $box.find('.jbox-help-button').outerHeight(true);
		var btnStyle = isIE ? 'line-height:19px;padding:0px 6px 0px 6px;': 'padding:0px 10px 0px 10px;';
		
		$.each(content, function(index, obj) {
			if (configs.isObject) {
				obj = $.extend({}, $.jBox.stateDefaults, obj)
			};
			content[index] = obj;
			if (obj.buttons == undefined) {
				obj.buttons = {}
			};
			var flg = false;
			$.each(obj.buttons, function(keyName, keyValue) {
				flg = true
			});
			var h_Val = 'auto';
			if (typeof options.height == 'number') {
				h_Val = options.height;
				if (isExt) {
					h_Val = h_Val - h1
				};
				if (flg) {
					h_Val = h_Val - h2
				};
				h_Val = (h_Val - 1) + 'px'
			};
			var loadingHtml = '';
			var paddingTop = '25px';
			if (!configs.isObject && isExt) {
				var setHeight = options.height;
				if (typeof options.height == 'number') {
					if (isExt) {
						setHeight = setHeight - h1
					};
					if (flg) {
						setHeight = setHeight - h2
					};
					paddingTop = ((setHeight / 5) * 2) + 'px';
					setHeight = (setHeight - 1) + 'px'
				};
				loadingHtml = ['<div id="jbox-content-loading" class="jbox-content-loading" style="min-height:70px;height:' + setHeight + '; text-align:center;">', '<div class="jbox-content-loading-image" style="display:block; margin:auto; width:220px; height:19px; padding-top: ' + paddingTop + ';"></div>', '</div>'].join('')
			};
			stateItem.push('<div id="jbox-state-' + index + '" class="jbox-state" style="display:none;">');
			stateItem.push('<div style="min-width:50px;width:' + (typeof options.width == 'number' ? options.width + 'px': 'auto') + '; height:' + h_Val + ';">' + loadingHtml + '<div id="jbox-content" class="jbox-content" style="height:' + h_Val + ';overflow:hidden;overflow-y:auto;">' + obj.content + '</div></div>');
			stateItem.push('<div class="jbox-button-panel" style="' + (flg ? '': 'display:none;') + '">');
			if (!options.isTip) {
				stateItem.push('<span class="jbox-bottom-text" style="float:left;display:block;line-height:25px;"></span>')
			};
			$.each(obj.buttons, function(keyName, keyValue) {
				stateItem.push('<button class="jbox-button" value="' + keyValue + '" style="' + btnStyle + '">' + keyName + '</button>')
			});
			stateItem.push('</div></div>')
		});
		$box.find('#jbox-states').html(stateItem.join('')).children('.jbox-state:first').css('display', 'block');
		if (isExt) {
			$box.find('#jbox-content').css({
				position: isIE6 ? "absolute": "fixed",
				left: -10000
			})
		};
		$.each(content, function(index, value) {
			var $stateItem = $box.find('#jbox-state-' + index);
			$stateItem.children('.jbox-button-panel').children('button').click(function() {
				var $jContent = $stateItem.find('#jbox-content');
				var button = value.buttons[$(this).text()];
				var el = {};
				$.each($box.find('#jbox-states :input').serializeArray(), function(i, o) {
					if (el[o.name] === undefined) {
						el[o.name] = o.value
					} else if (typeof el[o.name] == Array) {
						el[o.name].push(o.value)
					} else {
						el[o.name] = [el[o.name], o.value]
					}
				});
				var flg = value.submit(button, $jContent, el);
				if (flg === undefined || flg) {
					closeBox();
				}
			}).bind('mousedown', function() {
				$(this).addClass('jbox-button-active')
			}).bind('mouseup', function() {
				$(this).removeClass('jbox-button-active')
			}).bind('mouseover', function() {
				$(this).addClass('jbox-button-hover')
			}).bind('mouseout', function() {
				$(this).removeClass('jbox-button-active').removeClass('jbox-button-hover')
			});
			$stateItem.find('.jbox-button-panel button:eq(' + value.buttonsFocus + ')').addClass('jbox-button-focus')
		});
		
		/*!静止定位*/
		var setFixed = function() {
			$struObj.css({
				top: $win.scrollTop()
			});
			if (options.isMessager) {
				$box.css({
					position: isIE6 ? "absolute": "fixed",
					right: 1,
					bottom: 1
				})
			}
		};
		
		var docWidth = function() {
			var value = $win.width();
			return document.body.clientWidth < value ? value: document.body.clientWidth
		};
		
		var docHeight = function() {
			var value = $win.height();
			return document.body.clientHeight < value ? value: document.body.clientHeight
		};
		
		/*!遮罩层触发事件（关闭窗口）*/
		var markEvent = function() {
			if (!options.showFade) {
				return
			};
			if (options.persistent) {
				var value = 0;
				$struObj.addClass('jbox-warning');
				var timer = setInterval(function() {
					$struObj.toggleClass('jbox-warning');
					if (value++>1) {
						clearInterval(timer);
						$struObj.removeClass('jbox-warning');
					}
				},
				100)
			} else {
				closeBox();
			}
		};
		/*!全局快捷键*/
		var onKeyDown = function(event) {
			if (options.isTip || options.isMessager) {
				return false;
			};
			var key = event.keyCode;
			if (key == 27) {
				closeBox();
			};
			if (key == 9) {
				var el = $(':input:enabled:visible');
				var Q = !event.shiftKey && event.target == el[el.length - 1];
				var R = event.shiftKey && event.target == el[0];
				if (Q || R) {
					setTimeout(function() {
						if (!el){return;}
						var S = el[el === true ? el.length - 1: 0];
						if (S){S.focus();}
					},10);
					return false;
				}
			}
		};
		/*!遮罩层*/
		var setMark = function() {
			if (options.showFade) {
				$fade.css({
					position: "absolute",
					height: options.isTip ? docHeight() : $win.height(),
					width: isIE6 ? $win.width() : "100%",
					top: 0,
					left: 0,
					right: 0,
					bottom: 0
				})
			}
		};
		/*!窗口定位*/
		var setPosition = function() {
			if (options.isMessager) {
				$box.css({
					position: isIE6 ? "absolute": "fixed",
					right: 1,
					bottom: 1
				})
			} else {
				$temp.css({
					top: options.top
				});
				$box.css({
					position: "absolute",
					top: $temp.offset().top + (options.isTip ? $win.scrollTop() : 0),
					left: (($win.width() - $box.outerWidth()) / 2)
				});
			};
			if ((options.showFade && !options.isTip) || (!options.showFade && !options.isTip && !options.isMessager)) {
				$struObj.css({
					position: isIE6 ? "absolute": "fixed",
					height: options.showFade ? $win.height() : 0,
					width: "100%",
					top: isIE6 ? $win.scrollTop() : 0,
					left: 0,
					right: 0,
					bottom: 0
				});
			};
			setMark();
		};
		/*!置顶对话框*/
		var zIndexTop = function() {
			options.zIndex = $.jBox.defaults.zIndex++;
			$struObj.css({
				zIndex: options.zIndex
			});
			$box.css({
				zIndex: options.zIndex + 1
			});
		};
		
		/*!窗口zIndex设置*/
		var zIndexSet = function() {
			options.zIndex = $.jBox.defaults.zIndex++;
			$struObj.css({
				zIndex: options.zIndex
			});
			$box.css({
				display: "none",
				zIndex: options.zIndex + 1
			});
			if (options.showFade) {
				$fade.css({
					display: "none",
					zIndex: options.zIndex,
					opacity: options.opacity
				});
			}
		};
		
		/*!拖拽子事件*/
		var _dragEvent = {}
		_dragEvent.onclone = function(event){
			var el = event.data;
			el.target.find('iframe').hide();
			if (options.dragClone) {
				el.target.prev().css({
					left: el.target.css('left'),
					top: el.target.css('top'),
					marginLeft: -2,
					marginTop: -2,
					width: el.target.width() + 2,
					height: el.target.height() + 2
				}).show();
			};
			return false;				
		};
		_dragEvent.onmove = function(event){
			var el = event.data;
			var left = el.startLeft + event.pageX - el.startX;
			var top = el.startTop + event.pageY - el.startY;
			if (options.dragLimit) {
				var oh = 1;
				var wh = document.documentElement.clientHeight - el.target.height() - 1;
				var ow = 1;
				var ww = document.documentElement.clientWidth - el.target.width() - 1;
				if (top < oh) top = oh + (options.dragClone ? 2: 0);
				if (top > wh) top = wh - (options.dragClone ? 2: 0);
				if (left < ow) left = ow + (options.dragClone ? 2: 0);
				if (left > ww) left = ww - (options.dragClone ? 2: 0);
			};
			if (options.dragClone) {
				el.target.prev().css({
					left: left,
					top: top
				})
			} else {
				el.target.css({
					left: left,
					top: top
				});
			};
			return false;
		};
			
		_dragEvent.onend = function(event){
			$(document).unbind('.draggable');
			if (options.dragClone) {
				var el = event.data.target.prev().hide();
				event.data.target.css({
					left: el.css('left'),
					top: el.css('top')
				}).find('iframe').show();
			} else {
				event.data.target.find('iframe').show();
			};
			return false;
		}

		//拖拽事件
		var dragEvent = function(event) {
			var el = event.data.target.position();
			var data = {
				target: event.data.target,
				startX: event.pageX,
				startY: event.pageY,
				startLeft: el.left,
				startTop: el.top
			};
			$(document).bind('mousedown.draggable', data, _dragEvent.onclone).bind('mousemove.draggable', data, _dragEvent.onmove).bind('mouseup.draggable', data, _dragEvent.onend);
		};
		
		/*!关闭对话框 */
		var closeBox = function() {
			if (!options.isTip && !options.isMessager) {
				if ($('.jbox-body').length == 1) {
					$(isIE ? 'html': 'body').removeAttr('style')
				};
				removeBox();
			} else {
				if (options.isTip) {
					var tip = $(document.body).data('tip');
					if (tip && tip.next == true) {
						$temp.css('top', tip.options.top);
						var top = $temp.offset().top + $win.scrollTop();
						if (top == $box.offset().top) {
							removeBox();
						} else {
							$box.find('#jbox-content').html(tip.options.content.substr(5)).end().css({
								left: (($win.width() - $box.outerWidth()) / 2)
							}).animate({
								top: top,
								opacity: 0.1
							},
							500, removeBox);
						}
					} else {
						$box.animate({
							top: '-=200',
							opacity: 0
						},
						500, removeBox);
					}
				} else {
					switch (options.showType) {
					case 'slide':
						$box.slideUp(options.showSpeed, removeBox);
						break;
					case 'fade':
						$box.fadeOut(options.showSpeed, removeBox);
						break;
					case 'show':
					default:
						$box.hide(options.showSpeed, removeBox);
						break
					}
				}
			}
		};
		
		/*!窗口移除*/
		var removeBox = function() {
			$win.unbind('resize', setMark);
			if (options.draggable && !options.isTip && !options.isMessager) {
				$box.find('.jbox-title-panel').unbind('mousedown', dragEvent)
			};
			if (configs.type != 'IFRAME') {
				$box.find('#jbox-iframe').attr({
					'src': 'about:blank'
				})
			};
			$box.html('').remove();
			if (isIE6 && !options.isTip) {
				$docBody.unbind('scroll', setFixed)
			};
			if (options.showFade) {
				$fade.fadeOut('fast',
				function() {
					$fade.unbind('click', markEvent).unbind('mousedown', zIndexTop).html('').remove()
				})
			};
			$struObj.unbind('keydown keypress', onKeyDown).html('').remove();
			if (isIE6 && options.showFade) {
				$('select').css('visibility', 'visible');
			};
			if (typeof options.closed == 'function') {
				options.closed();
			}
		};
		
		/*!自动关闭*/ 
		var autoClosing = function() {
			if (options.timeout > 0) {
				$box.data('autoClosing', window.setTimeout(closeBox, options.timeout));
				if (options.isMessager) {
					$box.hover(function() {
						window.clearTimeout($box.data('autoClosing'))
					}, function() {
						$box.data('autoClosing', window.setTimeout(closeBox, options.timeout))
					})
				}
			}
		};
		
		/*!loaded接口：加载完成返回对象*/
		var loadedFn = function() {
			if (typeof options.loaded == 'function') {
				options.loaded($box.find('.jbox-state:visible').find('.jbox-content'))
			}
		};
		if (!configs.isObject) {
			switch (configs.type) {
			case "GET":
			case "POST":
				$.ajax({
					type:
					configs.type,
					url: configs.url,
					data: options.ajaxData == undefined ? {}: options.ajaxData,
					dataType: 'html',
					cache: false,
					success: function(data, textStatus) {
						$box.find('#jbox-content').css({
							position: "static"
						}).html(data).show().prev().hide();
						loadedFn();
					},
					error: function() {
						$box.find('#jbox-content-loading').html('<div style="padding-top:50px;padding-bottom:50px;text-align:center;">Loading Error.</div>')
					}
				});
				break;
			case "IFRAME":
				$box.find('#jbox-iframe').attr({
					'src':
					configs.url
				}).bind("load", function(event) {
					$(this).parent().css({
						position: "static"
					}).show().prev().hide();
					$box.find('#jbox-states .jbox-state:first .jbox-button-focus').focus();
					loadedFn()
				});
				break;
			default:
				$box.find('#jbox-content').show();
				break;
			}
		};
		setPosition();
		zIndexSet();
		if (isIE6 && !options.isTip) {
			$win.scroll(setFixed);
		};
		if (options.showFade) {
			$fade.click(markEvent);
		};
		$win.resize(setMark);
		$struObj.bind('keydown keypress', onKeyDown);
		$box.find('.jbox-close').click(closeBox);
		if (options.showFade) {
			$fade.fadeIn('fast');
		};
		var showType = 'show';
		if (options.showType == 'slide') {
			showType = 'slideDown';
		} else if (options.showType == 'fade') {
			showType = 'fadeIn';
		};
		if (options.isMessager) {
			$box[showType](options.showSpeed, autoClosing);
		} else {
			var tip = $(document.body).data('tip');
			if (tip && tip.next == true) {
				$(document.body).data('tip', {
					next: false,
					options: {}
				});
				$box.css('display', '');
			} else {
				if (!configs.isObject && isExt) {
					$box[showType](options.showSpeed);
				} else {
					$box[showType](options.showSpeed, loadedFn);
				}
			}
		};
		if (!options.isTip) {
			$box.find('.jbox-bottom-text').html(options.bottomText);
		} else {
			$box.find('.jbox-container,.jbox-content').addClass('jbox-tip-color');
		};
		if (configs.type != 'IFRAME') {
			$box.find('#jbox-states .jbox-state:first .jbox-button-focus').focus();
		} else {
			$box.focus();
		};
		if (!options.isMessager) {
			autoClosing();
		};
		$struObj.bind('mousedown', zIndexTop);
		if (options.draggable && !options.isTip && !options.isMessager) {
			$box.find('.jbox-title-panel').bind('mousedown', {
				target: $box
			},
			dragEvent).css('cursor', 'move');
		};
		return $struObj;
	};
	$.jBox.version = 2.3;
	$.jBox.defaults = {
		id: null,
		top: "15%",
		zIndex: 1984,
		border: 5,
		opacity: 0.1,
		timeout: 0,
		showType: 'fade',
		showSpeed: 'fast',
		showIcon: true,
		showClose: true,
		draggable: true,
		dragLimit: true,
		dragClone: false,
		persistent: true,
		showScrolling: true,
		ajaxData: {},
		iframeScrolling: 'auto',
		title: 'jBox',
		width: 350,
		height: 'auto',
		bottomText: '',
		buttons: {
			'确定': 'ok'
		},
		buttonsFocus: 0,
		loaded: function(h) {},
		submit: function(v, h, f) {
			return true
		},
		closed: function() {}
	};
	$.jBox.stateDefaults = {
		content: '',
		buttons: {
			'确定': 'ok'
		},
		buttonsFocus: 0,
		submit: function(v, h, f) {
			return true
		}
	};
	$.jBox.tipDefaults = {
		content: '',
		icon: 'info',
		top: '40%',
		width: 'auto',
		height: 'auto',
		opacity: 0,
		timeout: 3000,
		closed: function() {}
	};
	$.jBox.messagerDefaults = {
		content: '',
		title: 'jBox',
		icon: 'none',
		width: 350,
		height: 'auto',
		timeout: 3000,
		showType: 'slide',
		showSpeed: 600,
		border: 0,
		buttons: {},
		buttonsFocus: 0,
		loaded: function() {},
		submit: function(v, h, f) {
			return true
		},
		closed: function() {}
	};
	$.jBox.languageDefaults = {
		close: '关闭',
		ok: '确定',
		yes: '是',
		no: '否',
		cancel: '取消'
	};
	$.jBox.setDefaults = function(configs) {
		$.jBox.defaults = $.extend({}, $.jBox.defaults, configs.defaults);
		$.jBox.stateDefaults = $.extend({}, $.jBox.stateDefaults, configs.stateDefaults);
		$.jBox.tipDefaults = $.extend({}, $.jBox.tipDefaults, configs.tipDefaults);
		$.jBox.messagerDefaults = $.extend({}, $.jBox.messagerDefaults, configs.messagerDefaults);
		$.jBox.languageDefaults = $.extend({}, $.jBox.languageDefaults, configs.languageDefaults)
	};
	$.jBox.getBox = function() {
		return $('.jbox-body').eq($('.jbox-body').length - 1)
	};
	$.jBox.getIframe = function(jBoxId) {
		var $obj = (typeof jBoxId == 'string') ? $('#' + jBoxId) : $.jBox.getBox();
		return $obj.find('#jbox-iframe').get(0)
	};
	$.jBox.getContent = function() {
		return $.jBox.getState().find('.jbox-content').html()
	};
	$.jBox.setContent = function(content) {
		return $.jBox.getState().find('.jbox-content').html(content)
	};
	$.jBox.getState = function(stateNmae) {
		if (stateNmae == undefined) {
			return $.jBox.getBox().find('.jbox-state:visible')
		} else {
			return $.jBox.getBox().find('#jbox-state-' + stateNmae)
		}
	};
	$.jBox.getStateName = function() {
		return $.jBox.getState().attr('id').replace('jbox-state-', '')
	};
	$.jBox.goToState = function(stateName, stateContent) {
		var $box = $.jBox.getBox();
		if ($box != undefined && $box != null) {
			var $stateItem;
			stateName = stateName || false;
			$box.find('.jbox-state').slideUp('fast');
			if (typeof stateName == 'string') {
				$stateItem = $box.find('#jbox-state-' + stateName)
			} else {
				$stateItem = stateName ? $box.find('.jbox-state:visible').next() : $box.find('.jbox-state:visible').prev()
			};
			$stateItem.slideDown(350, function() {
				window.setTimeout(function() {
					$stateItem.find('.jbox-button-focus').focus();
					if (stateContent != undefined) {
						$stateItem.find('.jbox-content').html(stateContent)
					}
				},
				20)
			})
		}
	};
	$.jBox.nextState = function(stateContent) {
		$.jBox.goToState(true, stateContent)
	};
	$.jBox.prevState = function(stateContent) {
		$.jBox.goToState(false, stateContent)
	};
	$.jBox.close = function(token, suffix) {
		token = token || false;
		suffix = suffix || 'body';
		if (typeof token == 'string') {
			$('#' + token).find('.jbox-close').click()
		} else {
			var $obj = $('.jbox-' + suffix);
			if (token) {
				for (var i = 0, size = $obj.length; i < size; ++i) {
					$obj.eq(i).find('.jbox-close').click()
				}
			} else {
				if ($obj.length > 0) {
					$obj.eq($obj.length - 1).find('.jbox-close').click()
				}
			}
		}
	};
	$.jBox.open = function(content, title, width, height, options) {
		var defaults = {
			content: content,
			title: title,
			width: width,
			height: height
		};
		options = $.extend({}, defaults, options);
		options = $.extend({}, $.jBox.defaults, options);
		$.jBox(options.content, options)
	};
	$.jBox.prompt = function(content, title, icon, options) {
		var defaults = {
			content: content,
			title: title,
			icon: icon,
			buttons: eval('({ "' + $.jBox.languageDefaults.ok + '": "ok" })')
		};
		options = $.extend({}, defaults, options);
		options = $.extend({}, $.jBox.defaults, options);
		if (options.border < 0) {
			options.border = 0
		};
		if (options.icon != 'info' && options.icon != 'warning' && options.icon != 'success' && options.icon != 'error' && options.icon != 'question') {
			padding = '';
			options.icon = 'none'
		};
		var h0 = options.title == undefined ? 10: 45;
		var iconStyle = options.icon == 'none' ? 'height:auto;': 'min-height:30px;' + (this.isIE6 ? 'height:auto !important;height:100%;_height:30px;': 'height:auto;');
		var contentHTML = [];
		contentHTML.push('html:');
		contentHTML.push('<div style="margin:10px;' + iconStyle + 'padding-left:' + (options.icon == 'none' ? 0: 40) + 'px;text-align:left;">');
		contentHTML.push('<span class="jbox-icon jbox-icon-' + options.icon + '" style="position:absolute; top:' + (h0 + options.border) + 'px;left:' + (10 + options.border) + 'px; width:32px; height:32px;"></span>');
		contentHTML.push(options.content);
		contentHTML.push('</div>');
		options.content = contentHTML.join('');
		$.jBox(options.content, options)
	};
	$.jBox.alert = function(content, title, options) {
		$.jBox.prompt(content, title, 'none', options)
	};
	$.jBox.info = function(content, title, options) {
		$.jBox.prompt(content, title, 'info', options)
	};
	$.jBox.success = function(content, title, options) {
		$.jBox.prompt(content, title, 'success', options)
	};
	$.jBox.error = function(content, title, options) {
		$.jBox.prompt(content, title, 'error', options)
	};
	$.jBox.confirm = function(content, title, submit, options) {
		var defaults = {
			buttons: eval('({ "' + $.jBox.languageDefaults.ok + '": "ok", "' + $.jBox.languageDefaults.cancel + '": "cancel" })')
		};
		if (submit != undefined && typeof submit == 'function') {
			defaults.submit = submit
		} else {
			defaults.submit = function(v, h, f) {
				return true
			}
		};
		options = $.extend({}, defaults, options);
		$.jBox.prompt(content, title, 'question', options)
	};
	$.jBox.warning = function(content, title, submit, options) {
		var defaults = {
			buttons: eval('({ "' + $.jBox.languageDefaults.yes + '": "yes", "' + $.jBox.languageDefaults.no + '": "no", "' + $.jBox.languageDefaults.cancel + '": "cancel" })')
		};
		if (submit != undefined && typeof submit == 'function') {
			defaults.submit = submit
		} else {
			defaults.submit = function(v, h, f) {
				return true
			}
		};
		options = $.extend({}, defaults, options);
		$.jBox.prompt(content, title, 'warning', options)
	};
	$.jBox.tip = function(content, icon, options) {
		var defaults = {
			content: content,
			icon: icon,
			opacity: 0,
			border: 0,
			showClose: false,
			buttons: {},
			isTip: true
		};
		if (defaults.icon == 'loading') {
			defaults.timeout = 0;
			defaults.opacity = 0.1
		};
		options = $.extend({}, defaults, options);
		options = $.extend({}, $.jBox.tipDefaults, options);
		options = $.extend({}, $.jBox.defaults, options);
		if (options.timeout < 0) {
			options.timeout = 0
		};
		if (options.border < 0) {
			options.border = 0
		};
		if (options.icon != 'info' && options.icon != 'warning' && options.icon != 'success' && options.icon != 'error' && options.icon != 'loading') {
			options.icon = 'info'
		};
		var contentHTML = [];
		contentHTML.push('html:');
		contentHTML.push('<div style="min-height:18px;height:auto;margin:10px;padding-left:30px;padding-top:0px;text-align:left;">');
		contentHTML.push('<span class="jbox-icon jbox-icon-' + options.icon + '" style="position:absolute;top:' + (4 + options.border) + 'px;left:' + (4 + options.border) + 'px; width:32px; height:32px;"></span>');
		contentHTML.push(options.content);
		contentHTML.push('</div>');
		options.content = contentHTML.join('');
		if ($('.jbox-tip').length > 0) {
			$(document.body).data('tip', {
				next: true,
				options: options
			});
			$.jBox.closeTip()
		};
		if (options.focusId != undefined) {
			$('#' + options.focusId).focus();
			top.$('#' + options.focusId).focus()
		};
		$.jBox(options.content, options)
	};
	$.jBox.closeTip = function() {
		$.jBox.close(false, 'tip')
	};
	$.jBox.messager = function(content, title, timeout, options) {
		$.jBox.closeMessager();
		var defaults = {
			content: content,
			title: title,
			timeout: (timeout == undefined ? $.jBox.messagerDefaults.timeout: timeout),
			opacity: 0,
			showClose: true,
			draggable: false,
			isMessager: true
		};
		options = $.extend({}, defaults, options);
		options = $.extend({}, $.jBox.messagerDefaults, options);
		var config = $.extend({}, $.jBox.defaults, {});
		config.title = null;
		options = $.extend({}, config, options);
		if (options.border < 0) {
			options.border = 0
		};
		if (options.icon != 'info' && options.icon != 'warning' && options.icon != 'success' && options.icon != 'error' && options.icon != 'question') {
			padding = '';
			options.icon = 'none'
		};
		var h0 = options.title == undefined ? 10: 35;
		var iconStyle = options.icon == 'none' ? 'height:auto;': 'min-height:30px;' + (this.isIE6 ? 'height:auto !important;height:100%;_height:30px;': 'height:auto;');
		var contentHTML = [];
		contentHTML.push('html:');
		contentHTML.push('<div style="margin:10px;' + iconStyle + 'padding-left:' + (options.icon == 'none' ? 0: 40) + 'px;text-align:left;">');
		contentHTML.push('<span class="jbox-icon jbox-icon-' + options.icon + '" style="position:absolute; top:' + (h0 + options.border) + 'px;left:' + (10 + options.border) + 'px; width:32px; height:32px;"></span>');
		contentHTML.push(options.content);
		contentHTML.push('</div>');
		options.content = contentHTML.join('');
		$.jBox(options.content, options)
	};
	$.jBox.closeMessager = function() {
		$.jBox.close(false, 'messager')
	};
	window.jBox = $.jBox
})(jQuery);

/* jBox 全局设置 */
var jBoxConfig = {};

jBoxConfig.defaults = {
    id: null, /* 在页面中的唯一id，如果为null则自动生成随机id,一个id只会显示一个jBox */
    top: '15%', /* 窗口离顶部的距离,可以是百分比或像素(如 '100px') */
    border: 5, /* 窗口的外边框像素大小,必须是0以上的整数 */
    opacity: 0.1, /* 窗口隔离层的透明度,如果设置为0,则不显示隔离层 */
    timeout: 0, /* 窗口显示多少毫秒后自动关闭,如果设置为0,则不自动关闭 */
    showType: 'fade', /* 窗口显示的类型,可选值有:show、fade、slide */
    showSpeed: 'fast', /* 窗口显示的速度,可选值有:'slow'、'fast'、表示毫秒的整数 */
    showIcon: true, /* 是否显示窗口标题的图标，true显示，false不显示，或自定义的CSS样式类名（以为图标为背景） */
    showClose: true, /* 是否显示窗口右上角的关闭按钮 */
    draggable: true, /* 是否可以拖动窗口 */
    dragLimit: true, /* 在可以拖动窗口的情况下，是否限制在可视范围 */
    dragClone: false, /* 在可以拖动窗口的情况下，鼠标按下时窗口是否克隆窗口 */
    persistent: true, /* 在显示隔离层的情况下，点击隔离层时，是否坚持窗口不关闭 */
    showScrolling: true, /* 是否显示浏览的滚动条 */
    ajaxData: {},  /* 在窗口内容使用get:或post:前缀标识的情况下，ajax post的数据，例如：{ id: 1 } 或 "id=1" */
    iframeScrolling: 'auto', /* 在窗口内容使用iframe:前缀标识的情况下，iframe的scrolling属性值，可选值有：'auto'、'yes'、'no' */

    title: 'jBox', /* 窗口的标题 */
    width: 350, /* 窗口的宽度，值为'auto'或表示像素的整数 */
    height: 'auto', /* 窗口的高度，值为'auto'或表示像素的整数 */
    bottomText: '', /* 窗口的按钮左边的内容，当没有按钮时此设置无效 */
    buttons: { '确定': 'ok' }, /* 窗口的按钮 */
    buttonsFocus: 0, /* 表示第几个按钮为默认按钮，索引从0开始 */
    loaded: function (h) {}, /* 窗口加载完成后执行的函数，需要注意的是，如果是ajax或iframe也是要等加载完http请求才算窗口加载完成，参数h表示窗口内容的jQuery对象 */
    submit: function (v, h, f) { return true; }, /* 点击窗口按钮后的回调函数，返回true时表示关闭窗口，参数有三个，v表示所点的按钮的返回值，h表示窗口内容的jQuery对象，f表示窗口内容里的form表单键值 */
    closed: function () { } /* 窗口关闭后执行的函数 */
};

jBoxConfig.stateDefaults = {
    content: '', /* 状态的内容，不支持前缀标识 */
    buttons: { '确定': 'ok' }, /* 状态的按钮 */
    buttonsFocus: 0, /* 表示第几个按钮为默认按钮，索引从0开始 */
    submit: function (v, h, f) { return true; } /* 点击状态按钮后的回调函数，返回true时表示关闭窗口，参数有三个，v表示所点的按钮的返回值，h表示窗口内容的jQuery对象，f表示窗口内容里的form表单键值 */
};

jBoxConfig.tipDefaults = {
    content: '', /* 提示的内容，不支持前缀标识 */
    icon: 'info', /* 提示的图标，可选值有'info'、'success'、'warning'、'error'、'loading'，默认值为'info'，当为'loading'时，timeout值会被设置为0，表示不会自动关闭。 */
    top: '40%', /* 提示离顶部的距离,可以是百分比或像素(如 '100px') */
    width: 'auto', /* 提示的高度，值为'auto'或表示像素的整数 */
    height: 'auto', /* 提示的高度，值为'auto'或表示像素的整数 */
    opacity: 0, /* 窗口隔离层的透明度,如果设置为0,则不显示隔离层 */
    timeout: 3000, /* 提示显示多少毫秒后自动关闭,必须是大于0的整数 */
    closed: function () { } /* 提示关闭后执行的函数 */
};

jBoxConfig.messagerDefaults = {
    content: '', /* 信息的内容，不支持前缀标识 */
    title: 'jBox', /* 信息的标题 */
    icon: 'none', /* 信息图标，值为'none'时为不显示图标，可选值有'none'、'info'、'question'、'success'、'warning'、'error' */
    width: 350, /* 信息的高度，值为'auto'或表示像素的整数 */
    height: 'auto', /* 信息的高度，值为'auto'或表示像素的整数 */
    timeout: 3000, /* 信息显示多少毫秒后自动关闭,如果设置为0,则不自动关闭 */
    showType: 'slide', /* 信息显示的类型,可选值有:show、fade、slide */
    showSpeed: 600, /* 信息显示的速度,可选值有:'slow'、'fast'、表示毫秒的整数 */
    border: 0, /* 信息的外边框像素大小,必须是0以上的整数 */
    buttons: {}, /* 信息的按钮 */
    buttonsFocus: 0, /* 表示第几个按钮为默认按钮，索引从0开始 */
    loaded: function (h) { }, /* 窗口加载完成后执行的函数，参数h表示窗口内容的jQuery对象 */
    submit: function (v, h, f) { return true; }, /* 点击信息按钮后的回调函数，返回true时表示关闭窗口，参数有三个，v表示所点的按钮的返回值，h表示窗口内容的jQuery对象，f表示窗口内容里的form表单键值 */
    closed: function () { } /* 信息关闭后执行的函数 */
};

jBoxConfig.errorDefaults = {
	title: '错误提示' /* 信息的标题 */
};

jBoxConfig.languageDefaults = {
    close: '关闭', /* 窗口右上角关闭按钮提示 */
    ok: '确定', /* $.jBox.prompt() 系列方法的“确定”按钮文字 */
    yes: '是', /* $.jBox.warning() 方法的“是”按钮文字 */
    no: '否', /* $.jBox.warning() 方法的“否”按钮文字 */
    cancel: '取消' /* $.jBox.confirm() 和 $.jBox.warning() 方法的“取消”按钮文字 */
};

$.jBox.setDefaults(jBoxConfig);